"""
公共使用的方式
"""
from flask import current_app
from flask import g
from flask import session
from functools import wraps


def index_class(index):
    """自定义过滤器"""
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ''


# 使用装饰器，封装用户登陆状态
def user_login_data(view_func):
    @wraps(view_func)
    def wrapper(*args, **kwargs):
        # 获取用户编号
        user_id = session.get("user_id")
        # 判断用户是否存在
        user = None
        if user_id:
            try:
                from info.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 封装用户数据到g
        g.user = user

        return view_func(*args, **kwargs)

    return wrapper
